import { API_URL } from '@/utils/config'
import {storageData} from '@/utils/storageData.js'


/** 
 * post方法，对应post请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export const request = {
	wrapperToken: function(url) {
		let accessToken = ''
		const user = storageData.getLoginUser()
		if(user && user.accessToken){
			accessToken = user.accessToken
		}
		let sign = '?'
		console.log(url.indexOf('\?'))
		if(url.indexOf('\?') >= 0) {
			sign = '&'
		}
		return url + sign + 'Authorization=' + accessToken
	},
	post: function(url, params) {
		console.log(API_URL)
		let accessToken = ''
		const user = storageData.getLoginUser()
		if(user && user.accessToken){
			accessToken = user.accessToken
		}
		return new Promise((resolve, reject) => {
			uni.request({
				url: API_URL + url,
				data: params,
				header: {
					'Content-Type'	: 'application/x-www-form-urlencoded',
					'Authorization'	: accessToken
				},
				method: "POST",
				success: (response) => {
					console.log(response)
					console.log('请求成功')
					let res = response.data
					if (response.statusCode === 200) {
						resolve(res)
					} else {
						reject(res)
						showError(res);
					}
				},
				fail: (error) => {
					console.log('请求失败')
					if (error && error.response) {
						reject(error.response)
						showError(error.response);
					}
				},
			});
		})
	},
	postJson: function(url, params) {
		let accessToken = ''
		const user = storageData.getLoginUser()
		if(user && user.accessToken){
			accessToken = user.accessToken
		}
		return new Promise((resolve, reject) => {
			uni.request({
				url: API_URL + url,
				data: params,
				header: {
					'Content-Type'	: 'application/json',
					'Authorization'	: accessToken
				},
				method: "POST",
				success: (response) => {
					console.log(response)
					console.log('请求成功')
					let res = response.data
					if (response.statusCode === 200) {
						resolve(res)
					} else {
						reject(res)
						showError(res);
					}
				},
				fail: (error) => {
					console.log('请求失败')
					if (error && error.response) {
						reject(error.response)
						showError(error.response);
					}
				},
			});
		})
	},
	
	put: function(url, params) {
		let accessToken = ''
		const user = storageData.getLoginUser()
		console.log('开始请求put')
		console.log(user)
		if(user && user.accessToken){
			accessToken = user.accessToken
		}
		console.log('accessToken:{}' , accessToken)
		return new Promise((resolve, reject) => {
			uni.request({
				url: API_URL + url,
				data: params,
				header: {
					'Content-Type'	: 'application/x-www-form-urlencoded',
					'Authorization'	: accessToken
				},
				method: "PUT",
				success: (response) => {
					console.log(response)
					console.log('请求成功')
					let res = response.data
					if (response.statusCode === 200) {
						resolve(res)
					} else {
						reject(res)
						showError(res);
					}
				},
				fail: (error) => {
					console.log('请求失败')
					if (error && error.response) {
						reject(error.response)
						showError(error.response);
					}
				},
			});
		})
	},
	
	putJson: function(url, params) {
		let accessToken = ''
		const user = storageData.getLoginUser()
		console.log('开始请求put')
		console.log(user)
		if(user && user.accessToken){
			accessToken = user.accessToken
		}
		console.log('accessToken:{}' , accessToken)
		return new Promise((resolve, reject) => {
			uni.request({
				url: API_URL + url,
				data: params,
				header: {
					'Content-Type'	: 'application/json',
					'Authorization'	: accessToken
				},
				method: "PUT",
				success: (response) => {
					console.log(response)
					console.log('请求成功')
					let res = response.data
					if (response.statusCode === 200) {
						resolve(res)
					} else {
						reject(res)
						showError(res);
					}
				},
				fail: (error) => {
					console.log('请求失败')
					if (error && error.response) {
						reject(error.response)
						showError(error.response);
					}
				},
			});
		})
	},
	
	delete: function(url) {
		let accessToken = ''
		const user = storageData.getLoginUser()
		if(user && user.accessToken){
			accessToken = user.accessToken
		}
		return new Promise((resolve, reject) => {
			uni.request({
				url: API_URL + url,
				header: {
					'Authorization'	: accessToken
				},
				method: "DELETE",
				success: (response) => {
					if (response.statusCode === 200) {
						resolve()
					} else {
						reject(res)
						showError(res);
					}
				},
				fail: (error) => {
					console.log('请求失败')
					if (error && error.response) {
						reject(error.response)
						showError(error.response);
					}
				},
			});
		})
	},
	
	get: function(url, params) {
		
		let accessToken = ''
		const user = storageData.getLoginUser()
		if(user && user.accessToken){
			accessToken = user.accessToken
		}
		return new Promise((resolve, reject) => {
			uni.request({
				url: API_URL + url,
				data: params,
				header: {
					'Accept': 'application/json',
					'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
					'Authorization': accessToken
				},
				method: "GET",
				success: (response) => {
					let res = response.data
					if (response.statusCode === 200) {
						resolve(res)
					} else {
						reject(res)
						showError(res);
					}
				},
				fail: (error) => {
					if (error && error.response) {
						reject(error.response)
						showError(error.response);
					}
				}
			});
		})
	},
	
	uploadAvatar: function(url) {
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: API_URL + '/wechat/avatar/upload',
				filePath: url,
				name: 'upfile',
				header: {
					'Accept': 'multipart/form-data'
				},
				success: (res) => {
					console.log(res.data)
					if (res.statusCode === 200) {
						resolve(res.data)
					} else {
						reject(res)
						showError(res)
					}
				},
				fail: (error) => {
					if (error && error.response) {
						reject(error.response)
						showError(error.response);
					}
				}
			})
		})
	},
	
	
	uploadImg: function(url) {
		if(url) {
			url = API_URL + url
		}else{
			url = API_URL + '/image/upload'
		}
		return new Promise((resolve, reject) => {
			uni.chooseImage({
				count: 1, //默认9
				sizeType: ['compressed'],
				sourceType: ['album' , 'camera'], //从相册选择
				success: (chooseImageRes) => {
					const tempFilePaths = chooseImageRes.tempFilePaths

					let accessToken = ''
					const user = storageData.getLoginUser()
					if(user && user.accessToken){
						accessToken = user.accessToken
					}
					
					let title = url.indexOf('aiScan') >= 0 ? 'AI大模型识别中' : ''
					
					uni.showLoading({
						title: title
					})
					uni.uploadFile({
						url: url,
						filePath: tempFilePaths[0],
						name: 'image',
						header: {
							'Accept': 'application/json',
							'Authorization': accessToken
						},
						success: (res) => {
							console.log(res.data)
							uni.hideLoading()
							if (res.statusCode === 200) {
								resolve(res.data)
							} else {
								reject(res)
								showError(res)
							}
						},
						fail: (error) => {
							uni.hideLoading()
							if (error && error.response) {
								reject(error.response)
								showError(error.response);
							}
						}
					})
				}
			})
		})
	}
} 

const showError = error => {
	let errorMsg = ''
	switch (error.status) {
		case 301:
					errorMsg = '未授权，请登录'
					break
			case 400:
					errorMsg = '请求参数错误'
					break 
			case 401:
					errorMsg = '未授权，请登录'
					break
			case 403:
					errorMsg = error.message
					break
			case 404:
					errorMsg = '请求地址不存在'
					break
			case 500:
					errorMsg = '服务器内部错误'
					break
			case 504:
					errorMsg = '请求超时'
					break
			default:
					errorMsg = "请求失败"
					break
	}
	uni.showToast({
			title: errorMsg,
			icon: 'none',
			duration: 3000,
			complete: function() {
					setTimeout(function() {
							uni.hideToast();
					}, 3000);
			}
	});
}
